package com.larissa.android.quiz.question;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

public class SqliteHelper extends SQLiteOpenHelper {


    private static final String DB_NAME = "question.db";
    private static final String TABLE_NAME_QUESTION = "question";
    private static final String CREATE_SQL = "create table " +
            TABLE_NAME_QUESTION + " (id integer primary key autoincrement, category text, text text, answer text, createTime text, uuid text);";

    public SqliteHelper(@Nullable Context context) {
        super(context, DB_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_SQL);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public void query() {
        List<Question> geo = QuestionRepository.getInstance().getGeoQuestions();
        List<Question> math = QuestionRepository.getInstance().getMathQuestions();
        geo.clear();
        math.clear();
        String query = "SELECT * FROM " + TABLE_NAME_QUESTION;
        Cursor cursor = getReadableDatabase().rawQuery(query, null);
        if (cursor.getCount() != 0) {
            cursor.moveToFirst();
            do {
                String category = cursor.getString(1);
                String text = cursor.getString(2);
                String answer = cursor.getString(3);
                String createTime = cursor.getString(4);
                UUID uuid = UUID.fromString(cursor.getString(5));
                Question question = new Question(category, text, answer, createTime, uuid);
                if (category.equals(Question.GEO)) {
                    geo.add(question);
                } else if (category.equals(Question.MATH)) {
                    math.add(question);
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    public void update() {
        Consumer<Question> consumer = question ->
        {
            ContentValues values = new ContentValues();
            values.put("category", question.getCategory());
            values.put("text", question.getQuestionText());
            values.put("answer", question.getQuestionAnswer());
            values.put("createTime", question.getCreateTime());
            values.put("uuid", question.getQuestionId().toString());
            getReadableDatabase().update(TABLE_NAME_QUESTION, values, "uuid=?", new String[]{question.getQuestionId().toString()});
        };
        QuestionRepository.getInstance().getQuestionsToBeUpdated().forEach(consumer);
        QuestionRepository.getInstance().getQuestionsToBeUpdated().clear();
    }

    public void insert() {
        Consumer<Question> consumer = question -> {
            ContentValues values = new ContentValues();
            values.put("category", question.getCategory());
            values.put("text", question.getQuestionText());
            values.put("answer", question.getQuestionAnswer());
            values.put("createTime", question.getCreateTime());
            values.put("uuid", question.getQuestionId().toString());
            getReadableDatabase().insert(TABLE_NAME_QUESTION, null, values);
        };
        QuestionRepository.getInstance().getQuestionsToBeInserted().forEach(consumer);
        QuestionRepository.getInstance().getQuestionsToBeInserted().clear();
    }

    public void delete() {
        Consumer<Question> consumer = question -> {
            getReadableDatabase().delete(TABLE_NAME_QUESTION, "uuid=?", new String[]{question.getQuestionId().toString()});
        };
        QuestionRepository.getInstance().getQuestionsToBeDeleted().forEach(consumer);
        QuestionRepository.getInstance().getQuestionsToBeDeleted().clear();
    }

}
